package com.AAL.DAO;

import java.sql.Date;
import java.util.List;

import org.hibernate.classic.Session;

import com.AAL.DAO.Interface.IOrderDAO;
import com.AAL.Model.City;
import com.AAL.Model.Dealer;
import com.AAL.Model.Employee;
import com.AAL.Model.Order;
import com.AAL.Model.RequestingDetail;
import com.AAL.Model.StatusOfOrder;
import com.AAL.Utils.HibernateUtil;

public class OrderDAO implements IOrderDAO {

	@Override
	public Order saveAndUpdateOrder(Order order) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		try {
			session.saveOrUpdate(order);
		} catch (Exception e) {
			return null;
		}
		session.getTransaction().commit();
		return order;
	}

	@Override
	public Order deleteOrder(int orderID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			Order order = (Order) session.load(Order.class, orderID);
			if (null != order) {
				session.delete(order);
			}
			session.getTransaction().commit();
			return order;
		} catch (Exception e) {
			e.printStackTrace();
			session.getTransaction().rollback();
			return null;
		}
	}

	@Override
	public Order getOrder(int orderID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Order order = (Order) session.load(Order.class, orderID);
		return order;
	}

	@Override
	public List<Order> getOrderByDealer(Dealer dealer, Date fromDate,
			Date toDate, StatusOfOrder status) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Order> getNewOrderByCity(City city, boolean isProcessed) {
		// TODO Auto-generated method stub
		return null;
	}

	// thu 3 ngay 29/03/2011
	// Huyen
	// Lay tat ca cac don dat hang
	@SuppressWarnings("unchecked")
	public List<Order> GetAllOrderByRegion(short regionID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Order> lstOrder = null;
		try {
			System.out.println("Chuan bi thuc hien cau lenh");			
			lstOrder = (List<Order>) session
			.createQuery(
					"from Order where city.region.regionID=:regionID")
			.setParameter("regionID",regionID).list();			
			System.out.println("Danh sach order:" + lstOrder);
		} catch (Exception e) {
			System.out.println("E:"+ e.getMessage());
			return null;
		}
		session.getTransaction().commit();
		return lstOrder;
	}

	@Override
	public List<Order> GetAllOrder() {
		// TODO Auto-generated method stub
		return null;
	}
}
